package com.le.tester.webpage.redis.redisAsMQ;

import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;

/**
 * 描述：消费redis中的消息
 */
@Component
public class RedisMqListener {
    private static final Logger LOG = LoggerFactory.getLogger(RedisMqListener.class);
    @Resource
    private RedisTemplate<String, Object> redisTemplate;

    //定时任务
    //@Scheduled(cron = "0/10 * * * * ?")
    public void execute() {
        //遵循先进先出的原则，右进左出 or 左进右出
        //String.valueOf()会把空的字符串变成"null" 而不是null,也是坑啊。以后还是使用（String)，再进行判空的操作;
        String obj = (String) redisTemplate.opsForList().leftPop("redisMqList");
        if (StringUtils.isBlank(obj)) {
            LOG.info("没有值了");
            return;
        }
        LOG.info("从redisMqList中获取到的消息为 = [" + (StringUtils.isBlank(obj) ? "" : obj) + "]");
    }
}
